package com.wswy.wzcx.widget.utils;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.blankj.utilcode.util.SizeUtils;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.wswy.wzcx.R;

public class SRLLoadingHeader extends FrameLayout implements RefreshHeader {

  private static final String TAG = "SRLLoadingHeader";

  private static final int JUMP_DURATION = 150;

  private ObjectAnimator animator;

  public SRLLoadingHeader(Context context) {
    this(context, null);
  }

  public SRLLoadingHeader(Context context,
      @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public SRLLoadingHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initViews(context);
  }


  private ImageView imageView ;

  private void initViews(Context context) {
    imageView = new ImageView(context);
    imageView.setImageResource(R.drawable.ic_loading);
    FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(SizeUtils.dp2px(24),SizeUtils.dp2px(24));
    lp.gravity = Gravity.CENTER;
//    lp.topMargin = SizeUtils.dp2px(10);
    addView(imageView,lp);
  }

  protected RefreshKernel mRefreshKernel;
  


  @NonNull
  @Override
  public View getView() {
    return this;
  }

  @NonNull
  @Override
  public SpinnerStyle getSpinnerStyle() {
    return SpinnerStyle.Translate;
  }

  @Override
  public void setPrimaryColors(int... colors) {

  }

  @Override
  public void onInitialized(@NonNull RefreshKernel kernel, int height, int extendHeight) {
    mRefreshKernel = kernel;
  }

  @Override
  public void onMoving(boolean isDragging, float percent, int offset, int height,
      int maxDragHeight) {
    if (isDragging){
      float r = 360*(((float)offset)/(height+maxDragHeight));

      imageView.setRotation(r);
    }

  }

//  @Override
//  public void onPulling(float percent, int offset, int height, int extendHeight) {
//
//    float r = 360*(((float)offset)/(height+extendHeight));
//
//    imageView.setRotation(r);
//
//  }


  @Override
  public void onReleased(RefreshLayout refreshLayout, int height, int extendHeight) {

//    if (mRefreshKernel != null) {
//      mRefreshKernel.setState(RefreshState.None);
//      //onReleased 的时候 调用 setState(RefreshState.None); 并不会立刻改变成 None
//      //而是先执行一个回弹动画，RefreshFinish 是介于 Refreshing 和 None 之间的状态
//      //RefreshFinish 用于在回弹动画结束时候能顺利改变为 None
//      mRefreshKernel.setState(RefreshState.RefreshFinish);
//    }
  }

  @Override
  public void onStartAnimator(@NonNull RefreshLayout refreshLayout, int height, int extendHeight) {

    float r = imageView.getRotation();

    if (animator != null && animator.isRunning()){
      animator.cancel();
    }

    animator = ObjectAnimator.ofFloat(imageView, View.ROTATION,r, r + 360);
    animator.setInterpolator(new LinearInterpolator());
    animator.setRepeatCount(ObjectAnimator.INFINITE);
    animator.setRepeatMode(ObjectAnimator.RESTART);
    animator.setDuration(666);
    animator.start();

  }


  private void end(){
    if (animator != null){
      animator.cancel();
    }
  }


  @Override
  public int onFinish(@NonNull RefreshLayout refreshLayout, boolean success) {
    return 500;
  }

  @Override
  public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

  }

  @Override
  public boolean isSupportHorizontalDrag() {
    return false;
  }

  @Override
  public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState,
      RefreshState newState) {

    switch (newState) {
      case None:
        break;
      case PullDownToRefresh:

        break;
      case PullDownCanceled:
        end();
        break;
      case Loading:
        break;
      case RefreshFinish:
        end();
        break;

    }

  }
}
